package designPatterns.chainOfResponsibility;

/**
 * 处理责任链
 *
 * @author zhanglinfeng
 * @date create in 2023/7/26 15:36
 */
public class HandlerChain {
    /** 头部责任链 */
    private Handler head = null;
    /** 尾部责任链 */
    private Handler tail = null;

    /**
     * 添加处理链
     *
     * @param handler 处理链
     */
    public void addHandler(Handler handler) {
        handler.setNext(null);
        //首次添加
        if (head == null) {
            head = handler;
            tail = handler;
            return;
        }
        //非首次添加
        tail.setNext(handler);
        tail = handler;
    }

    /**
     * 处理
     */
    public void handle() {
        if (head != null) {
            head.handle();
        }
    }
}
